package com.ict.txmsv2.services;

import com.ict.txmsv2.dto.StudentInfoQueryDto;
import com.ict.txmsv2.mapper.ProjectEstablishMapper;
import com.ict.txmsv2.model.CompetitionPrize;
import com.ict.txmsv2.model.ProjectEstablish;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.RespPageBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author lht
 * @date 2021/4/22 - 15:41
 * @description: 课题立项服务类
 */
@Service
public class ProjectEstablishService {

    @Resource
    private ProjectEstablishMapper projectEstablishMapper;

    /**
     * 保存一个课题立项数据
     * @param projectEstablish
     * @return
     */
    public RespBean save(ProjectEstablish projectEstablish) {
        if (projectEstablishMapper.save(projectEstablish) != 0) {
            return RespBean.ok("保存成功！");
        }
        return RespBean.error("保存失败！");
    }

    public RespPageBean list(Integer page, Integer size, StudentInfoQueryDto queryDto) {
        if (page != null && page > 0) {
            page = (page - 1) * size;
        }
        RespPageBean resp = new RespPageBean();
        List<ProjectEstablish> list = projectEstablishMapper.list(page, size, queryDto);
        resp.setData(list);
        resp.setTotal(projectEstablishMapper.getTotal(queryDto));
        return resp;
    }

    public RespBean deleteProjectEstablishById(Integer id) {
        if(projectEstablishMapper.delete(id) != 0) {
            return RespBean.ok("删除成功!");
        }
        return RespBean.error("删除失败！");
    }

    public RespBean updateProjectEstablish(ProjectEstablish projectEstablish) {
        if(projectEstablishMapper.update(projectEstablish) != 0) {
            return RespBean.ok("更新成功！");
        }
        return RespBean.error("更新失败！");
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean batchSave(List<ProjectEstablish> list) {
        return projectEstablishMapper.batchSave(list) != 0;
    }

    public List<ProjectEstablish> getAllProjectEstablishInfo() {
        return projectEstablishMapper.getAllProjectEstablishInfo();
    }
}
